//
//  EditPasswordViewController.m
//  KIASupervisory
//
//  Created by tan_xin on 14-6-30.
//  Copyright (c) 2014年 MyCompany. All rights reserved.
//

#import "EditPasswordViewController.h"
#import "MTStatusBarOverlay.h"

@interface EditPasswordViewController ()

@property (weak, nonatomic) IBOutlet UITextField *oldPasswordTf;
@property (weak, nonatomic) IBOutlet UITextField *resetPswTf;
@property (weak, nonatomic) IBOutlet UITextField *confirmResetPswTf;

- (IBAction)editPasswordAction:(id)sender;

@end

@implementation EditPasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.showLeftBtn = YES;
    self.showRightBtn = NO;
    self.title = @"修改密码";
    
    [super viewWillAppear:animated];
}

- (void)leftNavItemAction:(id)sender {
    AppDelegate *dele = [AppDelegate sharedDelegate];
    DDMenuController *menuVC = dele.ddLeftMenu;
    [menuVC showLeft:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.inputAccessoryView == nil) {
        UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
        [topView setBarStyle:UIBarStyleDefault];
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(320-42, 3, 40, 26);
        [button setTitle:@"完成" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [button addTarget:self action:@selector(dismissKeyBoard:) forControlEvents:UIControlEventTouchUpInside];
        
        [topView addSubview:button];
        
        [textField setInputAccessoryView:topView];
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSInteger tag = textField.tag;
    
    NSString *oldPsw = self.oldPasswordTf.text;
    NSString *newPsw = self.resetPswTf.text;
    NSString *confirmPsw = self.confirmResetPswTf.text;
    
    if (tag == 100) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *correctOldPsw = [defaults objectForKey:@"password"];
        
        if ([oldPsw length] == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:@"原始密码不能为空，请输入"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles: nil];
            [alert show];
            
        } else if (![correctOldPsw isEqualToString:oldPsw]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:@"原始密码不正确，请重新输入"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles: nil];
            [alert show];
        }
    } else if (tag == 101) {
        if ([newPsw length] == 0) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:@"新密码不能为空，请输入"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles: nil];
            [alert show];
        }
    } else if (tag == 102) {
        if (![newPsw isEqualToString:confirmPsw]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:@"新设密码与确认密码不一致，请核实后重新输入"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles: nil];
            [alert show];
        }
    }
    
}

- (void)dismissKeyBoard:(id)sender {
    [self.view endEditing:YES];
}

- (IBAction)editPasswordAction:(id)sender {
    [self.view endEditing:YES];
    
    NSString *oldPsw = self.oldPasswordTf.text;
    NSString *newPsw = self.resetPswTf.text;
    NSString *confirmPsw = self.confirmResetPswTf.text;
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *correctOldPsw = [defaults objectForKey:@"password"];
    
    if ([oldPsw length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"原始密码不能为空，请输入"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
        [alert show];
    } else if (![correctOldPsw isEqualToString:oldPsw]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"原始密码不正确，请重新输入"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
        [alert show];
    } else if ([newPsw length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"新密码不能为空，请输入"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
        [alert show];
    } else if ([confirmPsw length] == 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"确认密码不能为空，请输入"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
        [alert show];
    } else if (![newPsw isEqualToString:confirmPsw]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"新设密码与确认密码不一致，请核实后重新输入"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
        [alert show];
    } else {
        UserInfo *user = [UserInfo instance];
        NSInteger flag = [[user.info objectForKeyOrNil:@"ryfl"] integerValue]; //角色
        NSString *gh = @"";
        if (flag == 2) { //领导
            gh = [user.info objectForKeyOrNil:@"yhm"];
        } else if (flag == 3) {
            gh = [user.info objectForKeyOrNil:@"gh"];
        }
        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                               gh,@"gh",
                               oldPsw,@"pwd",
                               newPsw,@"newPwd"
                               , nil];
        [self.networkManager requestWithMethod:modifyPwd parameter:param delegate:self];
        [self.HUD show:YES];
    }
}

#pragma mark - NetworkManagerDelegate
- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    
    if ([data isKindOfClass:[NSDictionary class]]) {
        int code = [[data objectForKeyOrNil:@"code"] integerValue];
        if (code == 0) { //请求成功
            if ([data count] > 0) {
                id dataObject = [data objectForKeyOrNil:@"data"];
                NSInteger resultCode = [[dataObject objectForKey:@"result"] integerValue];
                if (resultCode == 1) { //提交成功
                    
//                    MTStatusBarOverlay *overlay = [MTStatusBarOverlay sharedInstance];
//                    overlay.animation = MTStatusBarOverlayAnimationFallDown;
//                    overlay.detailViewMode = MTDetailViewModeDetailText;
//                    overlay.delegate = nil;
//                    overlay.progress = 0.0;
//                    [overlay postFinishMessage:@"密码修改成功！" duration:1.6 animated:YES];
//                    overlay.progress = 1.8;
                    
                    [self showAlertViewWithMessage:@"密码修改成功"];
                    
//                    [self performSelector:@selector(showErandReport) withObject:nil afterDelay:1.8];
                }
            }
        }
    }
    
}

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFailedWithError:(NSError *)error {
    
    [self.HUD hide:YES];
    
    NSDictionary *errorInfo = error.userInfo;
    NSString *msg = [errorInfo objectForKeyOrNil:@"errorMsg"];
    if ([msg length] == 0) {
        msg = @"密码修改失败，请稍后重试";
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                    message:msg
                                                   delegate:nil
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil];
    [alert show];
}

@end

